$(function () {
    layui.use(['element', 'layer', 'util'], function () {
        var element = layui.element
            , layer = layui.layer
            , util = layui.util
            , $ = layui.$;

        //头部事件
        util.event('lay-header-event', {
            //左侧菜单事件
            menuLeft: function (othis) {
                layer.msg('展开左侧菜单的操作', { icon: 0 });
            }
        });

        getUserInfo()

        // 退出登录
        $('.logout').on('click', function () {
            layer.confirm('确定退出登录吗？', { icon: 3, title: '提示' }, function (index) {
                localStorage.removeItem('token')
                location.href = '/login.html'
                layer.close(index);
            });
        })
    })
});
function getUserInfo () {
    // 发送axios请求
    axios({
        url: '/my/userinfo'
    }).then(({ data: res }) => {
        if (res.status != 0) return layer.msg(res.message)
        // 渲染头像
        let user = res.data
        let name = user.nickname || user.username;
        if (user.user_pic != null) {
            $('.text-avatar').hide()
            $('.layui-nav-img').show().attr('src', user.user_pic)
        } else {
            $('.text-avatar').html(name[0].toUpperCase())
        }
        $('.welcome').html(`欢迎  ${name}`)
    })
}